package edu.alaska.uaa.cs401.fgm;

import java.awt.Container;
import java.awt.EventQueue;
import java.awt.Font;
import java.io.IOException;
import java.util.prefs.Preferences;

import javax.swing.JFrame;
import javax.swing.JTextPane;

/**
 * 
 * @author James AND MATT!! YAY
 * 
 * Tests
 * 
 * When the UI opens, a server should be spawned capable of accepting two TCP connections
 * which will then manipulate the internal game board which will also display on the screen
 * whenever a change is made.  THE END
 * oh it'll say "white wins" or "black wins" when one wins.
 *
 */

public class FanoronaServerFrame extends JFrame implements FanoronaBoardDisplayerInterface {
	/**
	 * 
	 */
	private static final long serialVersionUID = 959419514198549359L;
	private final int FRAMEW = 500;
	private final int FRAMEH = 500;
	JTextPane textPane = new JTextPane();
	FanoronaBoardInterface board = new FanoronaLinkBoard();
	FanoronaServer server = null;
    Preferences prefs = Preferences.userNodeForPackage(this.getClass());
	
	public FanoronaServerFrame()
	{
//		String loggingDirectory = prefs.get("loggingDirectory", null);
//		if (loggingDirectory == null) {
//			JFileChooser chooser = new JFileChooser();
//			chooser.show
//		}
		
		
		setTitle("Fanorona Server");
		setSize(FRAMEW, FRAMEH);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		textPane.setFont(new Font("Courier", Font.PLAIN,14));
		
		Container contentPane = getContentPane();
		textPane.setText("Welcome!");
		contentPane.add(textPane, "Center");
	}
	public FanoronaServerFrame setServer(FanoronaServer server) {
		this.server = server;
		server.setDisplayer(this);
		return this;
	}
	public void update() {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				System.out.println("updating");
				FanoronaBoardInterface b = server.getBoard();
				String txt = server.getStatus(FanoronaServer.StatusPriority.HIGH);
				txt += "\n";
				FanoronaColor winner = b.getWinner();
				txt += " |012345678\n";
				txt += "-----------\n";
				String sb = server.getBoard().stringBoard();
				String [] sbs = sb.split("\n");
				int i = 4;
				for (String line : sbs) {
					txt += String.format("%d|%s\n",i--,line);
				}
				if (winner != null) {
					switch (winner) {
					case White:
						txt += "White wins.\n";
						break;
					case Black:
						txt += "Black wins.\n";
						break;
					}
				}
				StringBuilder builder = new StringBuilder();
				for (String q : server.getStatusArray(FanoronaServer.StatusPriority.HIGH)) {
					builder.insert(0, q);
					builder.insert(0,'\n');
				}
				textPane.setText(txt+"----PAST STATUS MESSAGES----"+builder);
			}
		});
	}
	static public void main(String[] args) {
		FanoronaServerFrame f;
		try {
			FanoronaServer s = new FanoronaServer();
			s.setDaemon(true);
			s.start();
			
			s.setLoggingDirectory(".");
			
			f = new FanoronaServerFrame().setServer(s);
			// K constant
			// K kinetic motion of fanorona pieces as I destroy game
			// K^12 <-- big number
			f.setVisible(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.err.format("Unable to start server!\n");
			e.printStackTrace();
			System.exit(1);
		}
	}
	public String requestNewLogFile() {
		return null;
		// if the file fails to save, this should pop up a new message requesting a new
		// filename
	}
}
